package com.altaic.structure.verbs ;

import com.altaic.structure.JapaneseVerb ;

public class JapaneseVerbUtils
{
    //http://www.epochrypha.com/japanese/materials/verbs/
    //shite imasu 2tane oluyor biri sillincek
    //TARGET_PRESENT_CONTINIOUS TARGET_TE_CONTINOUS

    public final static String TARGET_SIMPLE_PRESENT = "TARGET_SIMPLE_PRESENT" ;//ikimasu +

    public final static String TARGET_SIMPLE_PAST = "TARGET_SIMPLE_PAST" ;//ikimasshita +

    public final static String TARGET_PRESENT_CONTINIOUS = "TARGET_PRESENT_CONTINIOUS" ;//itte imasu +

    public final static String TARGET_WISH = "TARGET_WISH" ;//ikitai +tai

    public final static String TARGET_LOOK_LIKE = "TARGET_WISH" ;//furi-soo  +soo 

    public final static String TARGET_EASY = "TARGET_EASY" ;//nomi-yasui +yasui

    public final static String TARGET_DIFFICULT = "TARGET_DIFFICULT" ;//nomi-nikui +nikui

    public final static String TARGET_BEGINNIG = "TARGET_BEGINNIG" ;//hataraki-hacimaru +hacimaru

    public final static String TARGET_ENDING = "TARGET_ENDING" ;//yomi-owarimasu +owarimasu

    public final static String TARGET_CONTUINING = "TARGET_CONTUINING" ;//nomi-tsuzukemasu +tzukuemasu

    public final static String TARGET_SURPLUS = "TARGET_SURPLUS" ;//tabe-sugimasu +sugimasu

    public final static String TARGET_TE_CONTINOUS_IMASU = "TARGET_TE_CONTINOUS_IMASU" ;//yonde-imasu +imasu

    public final static String TARGET_TE_CONTINOUS_ARIMASU = "TARGET_TE_CONTINOUS_ARIMASU" ;//akete-arimasu +arimasu  mado ga akete arimasu( passive )

    public final static String TARGET_TE_OKU = "TARGET_TE_OKU" ;//katte okimasu   "prepare for"

    public final static String TARGET_TE_SHIMASU = "TARGET_TE_SHIMASU" ;//nemutte shimaimasu

    public final static String TARGET_TE_MIRU = "TARGET_TE_MIRU" ;//tabete mite

    public final static String TARGET_TE_KURU = "TARGET_TE_KURU" ;//katte kimasu

    public final static String TARGET_TE_IKU = "TARGET_TE_IKU" ;//motte ikimasu

    public final static String TARGET_TE_KUDASAI = "TARGET_TE_KUDASAI" ;//tabete kudasai

    public final static String TARGET_TE_HOSHII= "TARGET_TE_HOSHII" ;//kite hoshii

    public final static String TARGET_PROBABILITY_1 = "TARGET_PROBABILITY_1" ;//iku hazu   +hazu

    public final static String TARGET_PROBABILITY_2 = "TARGET_PROBABILITY_2" ;//iku KAMOSHIREMASEN +KAMOSHIREMASEN

    public final static String[ ] TARGETS = new String[ ] {
        TARGET_SIMPLE_PRESENT,
        TARGET_SIMPLE_PAST,
        TARGET_PRESENT_CONTINIOUS,
        TARGET_WISH,
        TARGET_LOOK_LIKE,
        TARGET_PROBABILITY_1,
        TARGET_PROBABILITY_2,
        TARGET_EASY,
        TARGET_DIFFICULT,
        TARGET_BEGINNIG,
        TARGET_ENDING,
        TARGET_CONTUINING,
        TARGET_SURPLUS,
        TARGET_TE_CONTINOUS_IMASU,
        TARGET_TE_CONTINOUS_ARIMASU,
        TARGET_TE_OKU,
        TARGET_TE_SHIMASU,
        TARGET_TE_MIRU,
        TARGET_TE_KURU,
        TARGET_TE_IKU,
        TARGET_TE_KUDASAI,        
        TARGET_TE_HOSHII } ;

    //----------------------------------------------------------------------------------------------------
    public final static String[ ] MASU_AUXULIARY_ADJECTIVES = new String[ ] { TARGET_WISH, TARGET_EASY, TARGET_DIFFICULT, TARGET_LOOK_LIKE } ;

    public final static String[ ] MASU_AUXULIARY_VERB = new String[ ] { TARGET_BEGINNIG, TARGET_ENDING, TARGET_CONTUINING, TARGET_SURPLUS } ;

    public final static String[ ] TE_AUXULIARY_ADJECTIVES = new String[ ] { TARGET_TE_KUDASAI, TARGET_TE_HOSHII } ;

    public final static String[ ] TE_AUXULIARY_VERB = new String[ ] { TARGET_TE_CONTINOUS_IMASU, TARGET_TE_CONTINOUS_ARIMASU, TARGET_TE_OKU, TARGET_TE_SHIMASU, TARGET_TE_MIRU, TARGET_TE_KURU, TARGET_TE_IKU, TARGET_TE_KUDASAI, TARGET_TE_KUDASAI } ;

    public final static String[ ][ ] POSTFIX_MAP = new String[ ][ ] {
        { TARGET_WISH, "tai" },
        { TARGET_EASY, "yasui" },
        { TARGET_EASY, "nikui" },
        { TARGET_EASY, "soo" },
        { TARGET_BEGINNIG, "hacime" },
        { TARGET_ENDING, "owari" },
        { TARGET_CONTUINING, "tsuzuke" },
        { TARGET_SURPLUS, "sugi" },
        { TARGET_PROBABILITY_1, " hazu" },
        { TARGET_PROBABILITY_2, " kamoshiremasen" },
        { TARGET_TE_CONTINOUS_IMASU, "i" },
        { TARGET_TE_CONTINOUS_ARIMASU, "a" },
        { TARGET_TE_OKU, "oki" },
        { TARGET_TE_SHIMASU, "shi" },
        { TARGET_TE_MIRU, "mi" },
        { TARGET_TE_MIRU, "ki" },
        { TARGET_TE_KURU, "ki" },
        { TARGET_TE_MIRU, "iki" },
        { TARGET_TE_KUDASAI, "kudasi" },
        { TARGET_TE_HOSHII, "hoshii" } } ;

    //----------------------------------------------------------------------------------------------------
    public final static String TIME_PAST = "TIME_PAST" ;

    public final static String TIME_SIMPLE_PRESENT = "TIME_SIMPLE_PRESENT" ;

    public final static String TIME_PRESENT_CONTINUOUS = "TIME_PRESENT_CONTINUOUS" ;

    public static String[ ] TIMES = new String[ ] { TIME_PAST, TIME_SIMPLE_PRESENT, TIME_PRESENT_CONTINUOUS } ;

    //----------------------------------------------------------------------------------------------------
    private static String BASE_A = "abase" ;

    private static String BASE_I = "ibase" ;

    private static String BASE_U = "ubase" ;

    private static String BASE_E = "ebase" ;

    private static String BASE_O = "obase" ;

    private static String BASE_TE = "tebase" ;

    private static String BASE_TA = "tabase" ;

    private static String[ ] BASES = new String[ ] { BASE_A, BASE_I, BASE_U, BASE_E, BASE_O, BASE_TE, BASE_TA } ;

    //--------------------------------------------
    public static String[ ] BASE_A_ARR = new String[ ] { "sa", "ka", "ga", "ba", "ma", "na", "ra", "ta", "wa" } ;

    public static String[ ] BASE_I_ARR = new String[ ] { "shi", "ki", "gi", "bi", "mi", "ni", "ri", "ti", "i" } ;

    public static String[ ] BASE_U_ARR = new String[ ] { "su", "ku", "gu", "bu", "mu", "nu", "ru", "tu", "u" } ;

    public static String[ ] BASE_E_ARR = new String[ ] { "se", "ke", "ge", "be", "me", "ne", "re", "te", "e" } ;

    public static String[ ] BASE_O_ARR = new String[ ] { "sou", "kou", "gou", "bou", "mou", "nou", "rou", "tou", "ou" } ;

    public static String[ ] BASE_TE_ARR = new String[ ] { "shite", "ite", "ide", "nde", "nde", "nde", "tte", "tte", "tte" } ;

    public static String[ ] BASE_TA_ARR = new String[ ] { "shita", "ita", "ida", "nda", "nda", "nda", "tta", "tta", "tta" } ;

    //---------------------------------
    public static String ENDING_SU = "su" ;//hanasu

    public static String ENDING_KU = "ku" ;//aruku 

    public static String ENDING_GU = "gu" ;//oyogu 

    public static String ENDING_BU = "bu" ;//yobu 

    public static String ENDING_MU = "mu" ;//nomu 

    public static String ENDING_NU = "nu" ;//shinu 

    public static String ENDING_RU = "ru" ;//tsukuru

    public static String ENDING_TSU = "tsu" ;//matsu 

    public static String ENDING_U = "u" ;//arau 

    public static String ENDING_E = "u" ;//tabe

    public static String[ ] ENDINGS = new String[ ] { ENDING_SU, ENDING_KU, ENDING_GU, ENDING_BU, ENDING_MU, ENDING_NU, ENDING_RU, ENDING_TSU, ENDING_U, ENDING_E } ;

    //------------------CONJUAGTES
    public static String CONJUGATE_TE = "CONJUGATE_TE" ;

    public static String CONJUGATE_TA = "CONJUGATE_TA" ;

    public static String CONJUGATE_NAI = "CONJUGATE_NAI" ;

    public static String CONJUGATES[] = { CONJUGATE_TE, CONJUGATE_TA, CONJUGATE_NAI } ;

    //    public static String renderVerb2( String word, String target, String time )
    //    {
    //        String verbType = getType( word ) ;
    //
    //        String baseType = "TODO" ;
    //
    //        if( TARGET_SIMPLE_PRESENT.equals( target ) || TARGET_SIMPLE_PAST.equals( target ) || TARGET_WISH.equals( target ) )
    //            baseType = BASE_I ;
    //
    //        if( TARGET_PRESENT_CONTINIOUS.equals( target ) )
    //            baseType = BASE_TE ;
    //
    //        if( TARGET_PROBABILITY_1.equals( target ) )
    //            baseType = BASE_U ;
    //
    //        if( TARGET_PROBABILITY_2.equals( target ) )
    //            baseType = BASE_U ;
    //
    //        String[ ] bases = getBaseArray( baseType ) ;
    //
    //        int conjugateIndex = getIndex( verbType ) ;
    //        String basePostfix = bases[ conjugateIndex ] ;
    //
    //        String conj = word + basePostfix ;
    //
    //        String timePostfix = "" ;
    //        if( TIME_PAST.equals( time ) )
    //            timePostfix = "mashita" ;
    //
    //        if( TIME_PRESENT_CONTINUOUS.equals( time ) )
    //            timePostfix = "masu" ;
    //
    //        if( TIME_SIMPLE_PRESENT.equals( time ) )
    //            timePostfix = "masu" ;
    //
    //        conj += timePostfix ;
    //
    //        return conj ;
    //    }

    public static String getConjugate( String conjugateType, String dictionaryForm, String time )
    {
        String verbType = getType( dictionaryForm ) ;
        return getConjugate( verbType, dictionaryForm, conjugateType, time ) ;
    }

    public static String getEnding( String time, String target )
    {

        if( TARGET_PROBABILITY_2.equals( target ) )
            return "" ;

        boolean isEndingWithVerb = true ;

        if( isMasuAuxiliaryAdjective( target ) )
            isEndingWithVerb = false ;

        if( isMasuAuxiliaryVerb( target ) )
            isEndingWithVerb = true ;
        
        if( isTeAuxiliaryAdjective( target ) )
            isEndingWithVerb = false ;

        if( isTeAuxiliaryVerb( target ) )
            isEndingWithVerb = true ;

        if( TARGET_PROBABILITY_1.equals( target ) )
            isEndingWithVerb = false ;

        String timePostfix = "" ;
        if( isEndingWithVerb )
        {
            if( TIME_PAST.equals( time ) )
                timePostfix = "mashita" ;

            if( TIME_PRESENT_CONTINUOUS.equals( time ) )
                timePostfix = " imasu" ;

            if( TIME_SIMPLE_PRESENT.equals( time ) )
                timePostfix = "masu" ;
        }
        else
        {
            if( TIME_PAST.equals( time ) )
                timePostfix = " deshita" ;

            if( TIME_PRESENT_CONTINUOUS.equals( time ) )
                timePostfix = " desu" ;

            if( TIME_SIMPLE_PRESENT.equals( time ) )
                timePostfix = " desu" ;
        }

        return timePostfix ;
    }

    protected static String getConjugate( String verbType, String dictionaryForm, String target, String time )
    {
        String stem = getBase( dictionaryForm, verbType ) ;
        String[ ] bases = getBaseForTarget( target ) ;

        String[ ] postfixs = getPostFix( target ) ;
        //String[ ] bases = getBaseArray( target ) ;

        int conjugateIndex = getIndex( verbType ) ;
        String basePostfix = postfixs == null ? "" : postfixs[ conjugateIndex ] ;

        String base = bases[ conjugateIndex ] ;
        String conj = stem + base + basePostfix ;

        String ending = getEnding( time, target ) ;

        conj += "-" + ending ;

        return conj ;
    }

    public static boolean isMasuAuxiliaryAdjective( String target )
    {
        for( String adj : MASU_AUXULIARY_ADJECTIVES )
        {
            if( adj.equals( target ) )
            {
                return true ;
            }
        }

        return false ;
    }

    public static boolean isMasuAuxiliaryVerb( String target )
    {
        for( String adj : MASU_AUXULIARY_VERB )
        {
            if( adj.equals( target ) )
            {
                return true ;
            }
        }

        return false ;
    }
    
    public static boolean isTeAuxiliaryAdjective( String target )
    {
        for( String adj : TE_AUXULIARY_ADJECTIVES )
        {
            if( adj.equals( target ) )
            {
                return true ;
            }
        }

        return false ;
    }

    public static boolean isTeAuxiliaryVerb( String target )
    {
        for( String adj : TE_AUXULIARY_VERB )
        {
            if( adj.equals( target ) )
            {
                return true ;
            }
        }

        return false ;
    }

    public static boolean isMasuAuxiliary( String target )
    {
        return isMasuAuxiliaryAdjective( target ) || isMasuAuxiliaryVerb( target ) ;
    }
    
    public static boolean isTeAuxiliary( String target )
    {
        return isTeAuxiliaryAdjective( target ) || isTeAuxiliaryVerb( target ) ;
    }

    public static String[ ] getBaseForTarget( String target )
    {
        if( isMasuAuxiliary( target ) )
        {
            return BASE_I_ARR ; //oyogi-tai return i-base
        }
        
        if( isTeAuxiliary( target ) )
        {
            return BASE_TE_ARR ; //oyonde return te-base
        }

        if( target.equals( TARGET_SIMPLE_PRESENT ) || target.equals( TARGET_SIMPLE_PAST ) )
            return BASE_I_ARR ; //oyogi-masu return i-base

        if( target.equals( TARGET_PRESENT_CONTINIOUS ) )
            return BASE_TE_ARR ; //itte imasu return te-base

        if( target.equals( TARGET_PROBABILITY_1 ) || target.equals( TARGET_PROBABILITY_2 ) )
            return BASE_U_ARR ; //oyogi-tai return i-base

        return null ;
    }

    public static String getBase( String dictionaryForm, String verbType )
    {
        int len = verbType.length( ) ;
        String base = dictionaryForm.substring( 0, dictionaryForm.length( ) - len ) ;

        return base ;
    }

    public static String[ ] getBaseArray( String baseType )
    {

        if( baseType.equals( BASE_A ) )
            return BASE_A_ARR ;

        if( baseType.equals( BASE_I ) )
            return BASE_I_ARR ;

        if( baseType.equals( BASE_U ) )
            return BASE_U_ARR ;

        if( baseType.equals( BASE_E ) )
            return BASE_E_ARR ;

        if( baseType.equals( BASE_O ) )
            return BASE_O_ARR ;

        if( baseType.equals( BASE_TE ) )
            return BASE_TE_ARR ;

        if( baseType.equals( BASE_TA ) )
            return BASE_TA_ARR ;

        return null ;
    }

    public static String[ ] getRepeatingArray( String el )
    {
        String[ ] repeatingArray = new String[ BASE_A_ARR.length ] ;
        for( int i = 0; i < BASE_A_ARR.length; i++ )
            repeatingArray[ i ] = el ;

        return repeatingArray ;
    }

    public static String[ ] getPostFix( String target )
    {
        for( String[ ] postFix : POSTFIX_MAP )
        {
            if( postFix[ 0 ].equals( target ) )
                return getRepeatingArray( postFix[ 1 ] ) ;

        }

        //        if( target.equals( TARGET_WISH ) )
        //            return getRepeatingArray( "tai" ) ;
        //
        //        if( target.equals( TARGET_PROBABILITY_1 ) )
        //            return getRepeatingArray( " hazu" ) ;
        //
        //        if( target.equals( TARGET_PROBABILITY_2 ) )
        //            return getRepeatingArray( " kamoshiramasen" ) ;

        return null ;
    }

    public static String getType( String dictionaryForm )
    {
        //hanasu
        if( dictionaryForm.endsWith( "su" ) )
            return ENDING_SU ;

        //aruku 
        if( dictionaryForm.endsWith( "ku" ) )
            return ENDING_KU ;

        //oyogu 
        if( dictionaryForm.endsWith( "gu" ) )
            return ENDING_GU ;

        //yobu 
        if( dictionaryForm.endsWith( "bu" ) )
            return ENDING_BU ;

        //nomu 
        if( dictionaryForm.endsWith( "mu" ) )
            return ENDING_MU ;

        //shinu 
        if( dictionaryForm.endsWith( "nu" ) )
            return ENDING_NU ;

        //tsukuru
        if( dictionaryForm.endsWith( "ru" ) )
            return ENDING_RU ;

        //matsu 
        if( dictionaryForm.endsWith( "tsu" ) )
            return ENDING_TSU ;

        //arau 
        if( dictionaryForm.endsWith( "u" ) )
            return ENDING_U ;

        return null ;
    }

    public static int getIndex( String dictionaryForm )
    {
        //hanasu
        if( dictionaryForm.endsWith( "su" ) )
            return 0 ;

        //aruku 
        if( dictionaryForm.endsWith( "ku" ) )
            return 1 ;

        //oyogu 
        if( dictionaryForm.endsWith( "gu" ) )
            return 2 ;

        //yobu 
        if( dictionaryForm.endsWith( "bu" ) )
            return 3 ;

        //nomu 
        if( dictionaryForm.endsWith( "mu" ) )
            return 4 ;

        //shinu 
        if( dictionaryForm.endsWith( "nu" ) )
            return 5 ;

        //tsukuru
        if( dictionaryForm.endsWith( "ru" ) )
            return 6 ;

        //matsu 
        if( dictionaryForm.endsWith( "tsu" ) )
            return 7 ;

        //arau 
        if( dictionaryForm.endsWith( "u" ) )
            return 8 ;

        return -1 ;
    }

    public static void main( String[ ] args )
    {
        String[ ] verbs = new String[ ] { "taberu", "hanasu", "aruku", "oyogu", "yobu", "nomu", "shinu", "tsukuru", "matsu", "arau" } ;

        for( String verb : verbs )
        {
            for( String b : BASES )
            {
                String res = getConjugate( b, verb, TIME_PRESENT_CONTINUOUS ) ;
                //System.err.print( res + "\t\t" ) ;

                JapaneseVerb jverb = new JapaneseVerb( verb, b, JapaneseVerbUtils.TIME_PRESENT_CONTINUOUS ) ;
                System.err.print( jverb.getConjugate( b ) + "\t\t" ) ;
            }
            System.err.print( "\n" ) ;
        }

    }
}
